<?


JCAT_Package::ImportClass( ROOT_PATH.'Classes/Coder/class.CoderDocBase.php') ;

class UICtrlContainerDoc extends JCAT_ArchiveDocument 
{
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function UICtrlContainerDoc( $sName )
	{
		$this->JCAT_ArchiveDocument( $sName ) ;
		
		$this->Set('Children',array()) ;
	}
	
	
	/**
	 * 添加一个 UICtrl 作为 子控件
	 * 
	 * @access	public
	 * @param	$sPath		string			what's this
	 * @return	bool
	 */
	public function AddUICtrl( $aLoadingDoc, $sPath )
	{		

		// 取得已有 
		$arrChildren = $this->Get('Children') ;
		JCAT_ASSERT::ASSERT_ARRAY($arrChildren) ;
		
		// 检查 重复
		foreach($arrChildren as $arrChild)
		{
			JCAT_ASSERT::ASSERT_ARRAY($arrChild) ;
			
			// 遇到重复
			if( $arrChild[TreeUICtrlChildren::CLM_PATH]==$sPath )
			{
				return false ;
			}
		}
		
		// 加入
		$arrNewChild[TreeUICtrlChildren::CLM_NAME] = $aLoadingDoc->Get('UICtrlName') ;
		$arrNewChild[TreeUICtrlChildren::CLM_KIND] = $aLoadingDoc->Get('UICtrlKind') ;
		$arrNewChild[TreeUICtrlChildren::CLM_PATH] = $sPath ;
		$arrNewChild[TreeUICtrlChildren::CLM_IMPLEMENTS] = ($aLoadingDoc->Get('UICtrlImplementType')==UICtrlDocBase::IMPTYPE_DERIVATION)?
					JCAT_Language::Sentence('子类化') :
					JCAT_Language::Sentence('实例化') ;
		$arrNewChild[TreeUICtrlChildren::CLM_EXCHANGE_TO] = '' ;
		$arrChildren[] = $arrNewChild ;

		// 保存
		$this->Set('Children',$arrChildren) ;
	
		return true ;
	}

	
	/**
	 * 将 子UICtrl 加载到 TreeView 上
	 * 
	 * @access	public
	 * @param	$aTreeModel				GtkTreeModel		what's this
	 * @param	$aParentItem=null		GtkTreeIter			what's this
	 * @return	void
	 */
	public function LoadChildrenUICtrl(GtkTreeModel $aTreeModel, GtkTreeIter $aParentItem=null)
	{
		$arrChildrenUICtrl = $this->Get('Children') ;
		if(!$arrChildrenUICtrl)
		{
			return ;
		}
		
		JCAT_ASSERT::ASSERT_ARRAY($arrChildrenUICtrl) ;

		foreach($arrChildrenUICtrl as $arrChild)
		{			
			// 插入 Item
			$aChildItem = $aTreeModel->append($aParentItem,array(
					$arrChild[TreeUICtrlChildren::CLM_NAME]
					, $arrChild[TreeUICtrlChildren::CLM_KIND]
					, $arrChild[TreeUICtrlChildren::CLM_IMPLEMENTS]
					, $arrChild[TreeUICtrlChildren::CLM_EXCHANGE_TO]
					, $arrChild[TreeUICtrlChildren::CLM_PATH]
			)) ;
			
			// 对于 Group 加载其子类
			if( JCAT_Global::IsKindOf($arrChild[TreeUICtrlChildren::CLM_KIND],'JCAT_UIGtkCtrlGroup') )
			{
				// 将记录中的 相对路径  换为 绝对路径
				$sRelativePath = $arrChild[TreeUICtrlChildren::CLM_PATH] ;
				if( is_file($sRelativePath) )
				{
					$sChildPath =& $sRelativePath ;
				}
				else
				{
					$sChildPath = dirname($this->GetPath()).'/'.$sRelativePath ;
				}
				
				// 恢复 子控件 的文档
				$aDocument = CoderDocBase::WakeupDocument($sChildPath) ;
				if( $aDocument )
				{
					JCAT_ASSERT::ASSERT_INSTANCE($aDocument,'UICtrlGtkGroupDoc') ;					
					$aChildDocument = $aDocument->GetUICtrlChildren() ;
					JCAT_ASSERT::ASSERT_INSTANCE($aChildDocument,__CLASS__) ;
					$aChildDocument->LoadChildrenUICtrl($aTreeModel,$aChildItem) ;					
				}
				else
				{
					$aTreeModel->append($aChildItem,array(JCAT_Language::Sentence('UICtrl控件存在错误'),'-','-','-')) ;
				}
			}
		}
		
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	bool
	 */
	public function RemoveUICtrl($nIdx)
	{
		$arrChildrenUICtrl = $this->Get('Children') ;
		if(!$arrChildrenUICtrl)
		{
			return false ;
		}
		
		if( !isset($arrChildrenUICtrl[$nIdx]) )
		{
			return false ;
		}
		
		// 删除
		unset($arrChildrenUICtrl[$nIdx]) ;
		
		// 重新整理 键
		$arrChildrenUICtrl = array_values($arrChildrenUICtrl) ;
		
		// 保存
		$this->Set('Children',$arrChildrenUICtrl) ;
		
		return true ;
	}

}
?>